<?php

namespace app\airobot\model;

use think\admin\Model;
use think\facade\Db;

class BaseModel extends Model
{
    public $table = '';
    public $model = null;

    public function __construct()
    {
        $this->model = Db::name($this->table);
        parent::__construct();
    }

    public function createInfo($createData)
    {
        return Db::table($this->table)->insertGetId($createData);
    }

    public function batchCreateInfo($createData)
    {
        return Db::table($this->table)->insertAll($createData);
    }

    public function deleteInfo($where)
    {
        return Db::table($this->table)->where($where)->delete();
    }

    public function batchDeleteInfo($where, array $whereIn)
    {
        return Db::table($this->table)->where($where)->whereIn($whereIn['field'], $whereIn['data'])->delete();
    }

    public function updateInfo($where, $updateData)
    {
        return Db::table($this->table)->where($where)->save($updateData);
    }

    public function queryInfo($where, $field = "*")
    {
        return Db::table($this->table)->field($field)->where($where)->find();
    }

    public function queryInfoByOrder($where, $field = "*", $order = "id asc")
    {
        return Db::table($this->table)->field($field)->where($where)->order($order)->find();
    }

    public function queryList(array $where, array $whereIn, array $whereNotIn, array $like, array $limit, string $group, $field = "*", $order = "id asc")
    {
        $query = Db::table($this->table)->field($field)->where($where);
        if (!empty($whereIn)) {
            $query = $query->whereIn($whereIn['field'], $whereIn['data']);
        }
        if (!empty($whereNotIn)) {
            $query = $query->whereNotIn($whereNotIn['field'], $whereNotIn['data']);
        }
        if (!empty($like)) {
            $query = $query->where($like);
        }
        if (!empty($limit)) {
            $query = $query->limit($limit['page'], $limit['count']);
        }
        if (!empty($group)) {
            $query = $query->group($group);
        }
        if (!empty($order)) {
            $query = $query->order($order);
        }

        return $query->select()->toArray();
    }

    public function getCount($where)
    {
        return Db::table($this->table)->where($where)->count();
    }

    public function getFields()
    {
        return Db::table($this->table)->getFields();
    }
}